#!/usr/bin/env python3
import subprocess
import venv
from pathlib import Path

_DIR = Path(__file__).parent
_PROGRAM_DIR = _DIR.parent
_VENV_DIR = _PROGRAM_DIR / ".venv"
_MODULE_DIR = _PROGRAM_DIR / "src" / "piper"
_TESTS_DIR = _PROGRAM_DIR / "tests"

_LINT_DIRS = [_MODULE_DIR, _TESTS_DIR]
_LINT_DIRS = [d.relative_to(_PROGRAM_DIR) for d in _LINT_DIRS]

if _VENV_DIR.exists():
    context = venv.EnvBuilder().ensure_directories(_VENV_DIR)
    python_exe = context.env_exe
else:
    python_exe = "python3"

subprocess.check_call(
    [python_exe, "-m", "black"] + _LINT_DIRS + ["--check"], cwd=_PROGRAM_DIR
)
subprocess.check_call(
    [python_exe, "-m", "isort"] + _LINT_DIRS + ["--check"], cwd=_PROGRAM_DIR
)
subprocess.check_call([python_exe, "-m", "flake8"] + _LINT_DIRS, cwd=_PROGRAM_DIR)
subprocess.check_call([python_exe, "-m", "pylint"] + _LINT_DIRS, cwd=_PROGRAM_DIR)
subprocess.check_call([python_exe, "-m", "mypy"] + _LINT_DIRS, cwd=_PROGRAM_DIR)
